<template>
  <div class="adm2">
    <div class="tabs">
      <!-- 表格 -->
      <el-container>
        <el-main>
          <el-header>
            <div class="book-title">类型管理</div>
            <div><el-button type="success" class="book-add" @click="dialogVisible2 = true">添加类型</el-button></div>
          </el-header>
          <el-table ref='checkall' :data='list' style='width: 100%;' max-height="480" border v-loading="isLoading"
            element-loading-text="加载中...">
            <!-- 名称 -->
            <el-table-column fixed prop='id' label='类型ID'></el-table-column>
            <el-table-column prop='type' label='类型名称'></el-table-column>
            <el-table-column label='操作' fixed="right">
              <template v-slot="scope">
                <el-button type="primary" @click="typeEdit(scope.row)">修改</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
      </el-container>

      <!-- 表单操作弹窗 -->
      <!-- 修改 -->
      <el-dialog v-model="dialogVisible" title="类型修改" width="30%">
        <el-form :model="formEdit" class="demo-form-inline" label-position="left">
          <el-form-item label="索引">
            <el-input v-model.lazy.trim="formEdit.id" placeholder="请输入id" type="number" />
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model.lazy.trim="formEdit.type" placeholder="请输入类型" />
          </el-form-item>

        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="typeUpdate">
              更新
            </el-button>
          </span>
        </template>
      </el-dialog>

      <!-- 添加 -->
      <el-dialog v-model="dialogVisible2" title="类型添加" width="30%">
        <el-form :model="formAdd" class="demo-form-inline" label-position="left">
          <el-form-item label="索引">
            <el-input v-model.lazy.trim="formAdd.id" disabled />
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model.lazy.trim="formAdd.type" placeholder="请输入类型" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible2 = false">取消</el-button>
            <el-button type="primary" @click="addCate">
              添加
            </el-button>
          </span>
        </template>
      </el-dialog>

    </div>

  </div>
</template>

<script>
import { showLoading } from "../../../utils/loading";
export default {
  name: 'type',
  data () {
    return {
      list: [],		//用来接收异地请求的数据
      right: '浏览中',	//状态
      cateid: '',		//类型id
      catetype: '',		//类型名称
      isLoading: true, //加载动画
      showLoading: showLoading, //加载动画函数
      dialogVisible: false, //修改弹窗
      dialogVisible2: false, // 添加弹窗
      // 表单数据
      formAdd: {
        id: 0,
        type: '',
        pid: 0,
        isdel: 0,
        prio: 0
      },
      // 表单数据
      formEdit: {
        id: 0,
        type: '',
        pid: 0,
        isdel: 0,
        prio: 0
      },
    }
  },
  methods: {
    // 添加类型
    addCate () {
      // 判断输入框是否为空
      if (this.formAdd.type !== '') {
        // 加载动画
        this.isLoading = true;
        // 发起post请求
        this.axios.post('/api/adm/type/add', this.$qs.stringify(this.formAdd), {
          headers: { 'Content-Type': 'application/x-www-form-urlencoded ; charset=UTF-8' },
        }).then((res) => {
          if (res.status === 200) {
            // 将心获取的数据重新赋值给admList
            this.list = res.data.data
            // 清空输入框
            this.formAdd.id = 0,
              this.formAdd.type = '',
              this.dialogVisible2 = false
            // 关闭加载动画
            this.showLoading();
            // 提示
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 1500
            });
          }
        }).catch((error) => {
          console.log(error)
        })
      } else {
        this.$message({
          message: '输入框不能为空',
          type: 'warning',
          duration: 1500
        });
      }
    },

    // 修改类型
    typeEdit (item) {
      // 打开 dialog
      this.dialogVisible = true;
      // 将当前行数据赋值给formEdit
      this.formEdit.id = item.id;
      this.formEdit.type = item.type;
    },
    // 更新
    typeUpdate () {
      // 判断输入框是否为空
      if (this.formEdit.type !== '' && this.formEdit.id !== '') {
        // 加载动画
        this.isLoading = true;
        // 发起post请求
        this.axios.post('/api/adm/type/add', this.$qs.stringify(this.formEdit), {
          headers: { 'Content-Type': 'application/x-www-form-urlencoded ; charset=UTF-8' },
        }).then((res) => {
          if (res.status === 200) {
            // 将新获取的数据重新赋值给list
            this.list = res.data.data
            // 清空输入框
            this.formEdit.id = '',
              this.formEdit.type = '',
              this.dialogVisible = false
            // 关闭加载动画
            this.showLoading();
            // 提示
            this.$message({
              message: '更新成功',
              type: 'success',
              duration: 1500
            });
          }
        }).catch((error) => {
          console.log(error)
        })
      } else {
        this.$message({
          message: '输入框不能为空',
          type: 'warning',
          duration: 1500
        });
      }
    },

  },

  created () {
    //发起异步请求
    this.axios.get('/api/adm/type/findAll').then((res) => {
      //判断状态
      if (res.status === 200) {
        //向表格渲染数据
        this.list = res.data.data
        this.showLoading();
      }
    }).catch((error) => {
      console.log(error)
    })
  }
}
</script>

<style scoped>
/* 分页 */
.book-pagination {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  margin-bottom: 10px;
}

.el-header {
  border: 1px solid gainsboro;
  display: flex;
  align-items: center;
  margin-top: -20px;
  justify-content: space-between;
  border-radius: 3px;
}

.el-header .book-title {
  font-size: 20px;
  margin-left: 20px;
  font-weight: bold;
}

.el-header .book-add {
  margin-right: 20px;
}

.el-button {
  height: 10px !important;
}
</style>







